package com.campus.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.api.util.UserContext;
import com.campus.campus.job.dto.RepairRequestDTO;
import com.campus.campus.job.entity.RepairRequest;
import com.campus.campus.mapper.RepairRequestMapper;
import com.campus.campus.service.IRepairRequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.exception.ServiceException;
import com.campus.common.repairTechnician.entity.RepairTechnician;
import com.campus.common.repairTechnician.mapper.RepairTechnicianMapper;
import com.campus.common.student.job.entity.Student;
import com.campus.common.student.mapper.StudentMapper;
import com.campus.common.user.entity.pojo.User;
import com.campus.common.user.mapper.AuthMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 维护报修表 服务实现类
 * </p>
 *
 * @author mwb
 * @since 2024-11-17
 */
@Service
public class RepairRequestServiceImpl extends ServiceImpl<RepairRequestMapper, RepairRequest> implements IRepairRequestService {

    @Autowired
    private RepairRequestMapper repairRequestMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private AuthMapper authMapper;

    @Autowired
    private RepairTechnicianMapper repairTechnicianMapper;

    /**
     * 报修申请
     */
    @Override
    public void repairRequest(RepairRequestDTO repairRequestDTO) {
        int userId = UserContext.getUserId();
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getUserId, userId));
        if (Optional.ofNullable(student).isEmpty()) {
            throw new ServiceException("未查询到该学生信息");
        }

        User user = authMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getId, student.getUserId()));
        if (Optional.ofNullable(user).isEmpty()) {
            throw new ServiceException("未查询到该用户信息");
        }

        RepairRequest repairRequest = new RepairRequest();
        BeanUtils.copyProperties(repairRequestDTO, repairRequest);
        System.out.println("repairRequest"+repairRequest.toString());

        repairRequest.setStudentId(student.getId());
        repairRequest.setRepairBy(user.getName());
        repairRequest.setContact(user.getContact());
        repairRequest.setTime(LocalDateTime.now());
        int row = repairRequestMapper.insert(repairRequest);
        if (row < 1) {
            throw new ServiceException("报修申请失败");
        }

    }


    /**
     * 我的报修列表
     */
    @Override
    public List<RepairRequest> myRepairDetail() {
        int userId = UserContext.getUserId();
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getUserId, userId));
        if (Optional.ofNullable(student).isEmpty()) {
            throw new ServiceException("未查询到该学生信息");
        }

        List<RepairRequest> list = repairRequestMapper.selectList(new LambdaQueryWrapper<RepairRequest>()
                .eq(RepairRequest::getStudentId, student.getId()));
        if (list.isEmpty()) {
            return new ArrayList<>();
        }

        return list;
    }


    /**
     * 取消报修
     */
    @Override
    public void cancelRepair(Integer id) {
        RepairRequest repairRequest = repairRequestMapper.selectById(id);
        if (Optional.ofNullable(repairRequest).isEmpty()) {
            throw new ServiceException("未查询到该报修信息");
        }

        repairRequest.setStatus(2);
        int row = repairRequestMapper.updateById(repairRequest);
        if (row < 1) {
            throw new ServiceException("取消报修失败");
        }
    }



    /**
     * 报修完成
     */
    @Override
    public void acceptRepair(Integer id) {
        int userId = UserContext.getUserId();
        //维修人员操作完成
        RepairTechnician repairTechnician = repairTechnicianMapper.selectOne(new LambdaQueryWrapper<RepairTechnician>()
                .eq(RepairTechnician::getUserId, userId));
        if (Optional.ofNullable(repairTechnician).isEmpty()) {
            throw new ServiceException("未查询到该维修人员信息");
        }

        RepairRequest repairRequest = repairRequestMapper.selectById(id);
        if (Optional.ofNullable(repairRequest).isEmpty()) {
            throw new ServiceException("未查询到该报修人员信息");
        }

        //修改状态
        repairRequest.setStatus(1);
        repairRequest.setRepairTechnicianId(repairTechnician.getId());
        repairRequest.setFinishTime(LocalDateTime.now());
        int row = repairRequestMapper.updateById(repairRequest);
        if (row < 1) {
            throw new ServiceException("完成报修失败");
        }
    }
}
